﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using studentskicentarETFa;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for DodavanjePredmeta.xaml
    /// </summary>
    public partial class DodavanjePredmeta : Window
    {
        public DataGrid PregledPredmetaGrid;

        public DodavanjePredmeta()
        {
            InitializeComponent();
        }
        
        public DodavanjePredmeta(DataGrid PregledPredmetaGrid)
        {
            InitializeComponent();
            this.PregledPredmetaGrid = PregledPredmetaGrid;
        }
        private void BindMyData()
        {
            MySqlConnection con = null;
            string connectionString = "server=localhost;user=root;pwd=;database=centar";
            con = new MySqlConnection(connectionString);
            try
            {
                con.Open();
                MySqlCommand comm = new MySqlCommand("SELECT * FROM Predmet", con);
                comm.CommandText = "SELECT * from Predmet";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm);
                da.Fill(ds);
                PregledPredmetaGrid.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (NazivTB.Text == "" || IDPredmetaTB.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("Unesite sve potrebne podatke", "Upozorenje", System.Windows.Forms.MessageBoxButtons.OKCancel);
            }
            else
            {
                Predmet pr = new Predmet(NazivTB.Text, IDPredmetaTB.Text);
                studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
                d.kreirajKonekciju("localhost", "Centar", "root", "");
                studentskicentarETFa.DAL.PredmetDAO p = new studentskicentarETFa.DAL.PredmetDAO();
                p.create(pr);
                System.Windows.MessageBox.Show("Uspjesno ste dodali predmet.");
                BindMyData();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void IDPredmetaTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            IDPredmetaTB.CharacterCasing= CharacterCasing.Upper;
        }
    }
}
